Skip to content

fix(uffd): log error and remove stale TODO on handle failure - #3132

Closed
AdaAibaby wants to merge 3 commits into
e2b-dev:mainfrom
AdaAibaby:fix/uffd-handler-failure-logging
Closed

AdaAibaby wants to merge 3 commits into
e2b-dev:mainfrom
AdaAibaby:fix/uffd-handler-failure-logging

Conversation

@AdaAibaby

@AdaAibaby AdaAibaby commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

fix #3131

What was wrong

The UFFD handle goroutine had a stale TODO comment:

// TODO: If the handle function fails, we should kill the sandbox
handleErr := u.handle(ctx, sandboxId, fdExit)

The sandbox is already killed when handle() fails — the comment was wrong and created a correctness trap. When u.exit.SetError() is called after a handle() failure, it closes the Exit().Done() channel. The lifecycle goroutine in sandbox.go (lines 1164–1174) is watching that channel and calls sbx.Stop() immediately:

// sandbox.go:1164–1174
select {
case <-fcUffd.Exit().Done():   // fires when u.exit.SetError() is called
case <-fcHandle.Exit.Done():
}
err := sbx.Stop(ctx)           // actively kills the Firecracker VM

There was also no log line when handle() failed, making UFFD failures invisible in production.

Changes

  • uffd.go: Remove the stale TODO. Add an error-level log line when handle() fails so failures are observable.
  • sandbox.go: Add a comment on the lifecycle goroutine's select block explaining that UFFD failure flows through Exit().Done() → sbx.Stop(), making the existing mechanism explicit.
  • uffd_test.go: Add unit tests covering handle failure behaviour: exit error propagation, readyCh closure, handler error state, initial state, and socket creation on Start().

/cc @jakubno @dobrac @ValentaTomas — Would appreciate your review on this change, thanks!

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds logging when the userfaultfd (uffd) handle function fails, indicating that the sandbox will be terminated, and introduces a comprehensive suite of unit tests for the uffd package. There are no review comments, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

adababys added 3 commits July 23, 2026 19:03
The UFFD handle goroutine had a TODO comment stating that the sandbox
should be killed when handle() fails. In fact, this is already
implemented: u.exit.SetError() triggers the sandbox exit watcher in
sandbox.go (line ~1093) which calls sbx.Stop().

- Remove the stale TODO comment
- Add error logging when handle() fails for better observability
- Add unit tests covering handle failure behavior (exit error
  propagation, readyCh closure, handler error state, initial state,
  socket creation)
The sandbox.go goroutine that monitors fcUffd.Exit().Done() was not
documented as the mechanism that terminates the VM when UFFD handle()
fails. Add a comment making this explicit so readers do not mistake the
gap described in the now-removed uffd.go TODO for a real missing piece.
@AdaAibaby
AdaAibaby force-pushed the fix/uffd-handler-failure-logging branch from 7b54ed7 to fc647fe Compare July 23, 2026 11:03
@ValentaTomas
ValentaTomas force-pushed the main branch 2 times, most recently from 5aad415 to d71980e Compare July 25, 2026 22:53
@ryan-sana

Copy link
Copy Markdown
Contributor

Thank you @AdaAibaby for tracing this through to the sandbox lifecycle watcher and showing that the sandbox is already stopped when the UFFD handler fails, so the TODO is misleading, and for adding the failure log and unit tests. This branch now conflicts with main after many changes to the sandbox and uffd code, and it has had no activity for over two months, so I'm closing it for now. If you'd like to pick it up again, a fresh PR against current main with the log line and the TODO removal would be welcome, and you're free to reopen this one too.

@ryan-sana ryan-sana closed this Oct 21, 2026
AdaAibaby pushed a commit to AdaAibaby/infra that referenced this pull request Nov 25, 2026
The uffd serve goroutine carried a stale TODO ("if the handle function fails,
we should kill the sandbox") and logged nothing when handle() failed, so a
userfaultfd failure was invisible in production.

- Remove the misleading TODO.
- Log the failure at ERROR, but only for a genuine serve-loop failure: the
  existing stopping-time close (net.ErrClosed / context.Canceled) already
  zeroes exitErr, so the log is gated on exitErr != nil and does not fire on
  normal sandbox stop.
- Add an accurate comment on the resume-path lifecycle watcher noting that a
  completed fcUffd.Exit() (which a serve-loop failure triggers) is turned into
  a sandbox stop there, so a dead UFFD handler does not leave a VM without a
  page-fault handler. Scoped to the resume path, which is the one that watches
  fcUffd.Exit().Done().

Rebased, trimmed replacement for e2b-dev#3132 (envd/sandbox code was reworked since).
Adds a focused test asserting a handle failure surfaces a non-nil uffd exit
error - the condition that gates the new log. Revisits e2b-dev#3131.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(orchestrator): terminate sandbox when UFFD handler fails

3 participants